package com.example.onlineeducation.ui.main

import android.content.Context
import android.content.Intent
import com.example.onlineeducation.App
import com.example.onlineeducation.R
import com.example.onlineeducation.base.BaseActivity
import com.example.onlineeducation.databinding.MainActivityBinding
import com.yc.jetpacklib.utils.YcSwitchFragmentUtil

class MainActivity : BaseActivity<MainActivityBinding>(MainActivityBinding::inflate) {

    private val mSwitchFragment by lazy { YcSwitchFragmentUtil() }

    companion object {
        fun newInstance(context: Context) {
            val intent = Intent(context, MainActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun MainActivityBinding.initView() {
        bottomNavigationView.itemIconTintList = null
        mSwitchFragment.setFragments(
            mapOf(
                R.id.homeFragmentMenu to MainHomeFragment(),
                R.id.topicFragmentMenu to MainTopicFragment(),
                R.id.offlineExamFragmentMenu to MainOfflineExamFragment(),
                R.id.mineFragmentMenu to MainMineFragment()
            )
        )
        bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation_items_offline_exam)
        mSwitchFragment.showFirstFragment(supportFragmentManager, R.id.fragmentContainer, R.id.homeFragmentMenu)

        App.mInstant.mViewModel.mProInfoEntity.observe {
            when (it?.hasOfflinePermission) {
                true -> {
                    bottomNavigationView.menu.getItem(2).isVisible = true
                    mSwitchFragment.showFragment(R.id.offlineExamFragmentMenu)
                }
                else -> {
                    bottomNavigationView.menu.getItem(2).isVisible = false
                    mSwitchFragment.hideFragment(R.id.offlineExamFragmentMenu)
                }
            }
        }

        bottomNavigationView.setOnNavigationItemSelectedListener {
            return@setOnNavigationItemSelectedListener if (mSwitchFragment.switchFragment(it.itemId)) {
                it.isChecked = true
                true
            } else {
                false
            }
        }
    }
}
